#!/bin/sh

revision="settrain v0.2.3 (2023-07-02 by MIB-Wiki)"
# use --help for more info

export PATH=:/proc/boot:/sbin:/bin:/usr/bin:/usr/sbin:/net/mmx/bin:/net/mmx/usr/bin:/net/mmx/usr/sbin:/net/mmx/sbin:/net/mmx/mnt/app/armle/bin:/net/mmx/mnt/app/armle/sbin:/net/mmx/mnt/app/armle/usr/bin:/net/mmx/mnt/app/armle/usr/sbin
export LD_LIBRARY_PATH=/net/mmx/mnt/app/root/lib-target:/net/mmx/mnt/eso/lib:/net/mmx/eso/lib:/net/mmx/mnt/app/usr/lib:/net/mmx/mnt/app/armle/lib:/net/mmx/mnt/app/armle/lib/dll:/net/mmx/mnt/app/armle/usr/lib
export IPL_CONFIG_DIR=/etc/eso/production

thisname="$(basename $0)"
thisdir="$(dirname $0)"

if [ -z $LOG ]; then
	. $thisdir/../config/GLOBALS
fi
echo -ne "$ME-$thisname---->\n" | $TEE -i -a $LOG

if [ -f $TMP/reboot.mib ]; then
	echo -ne "Aborted because the reboot is running...\n" | $TEE -a $LOG
	return 2> /dev/null
fi

MIBCAP=3
. $thisdir/../config/MIBCHECK

if [ "$2" = "-noboot" ]; then
	noboot=$2
else
	noboot=""
fi

#Get the real train not from GLOBALS but from EEPROM as it can be changed already
TRAINVERSION="$($E2P r 3A0 19 | $SED -rn 's/^0x\S+\W+(.*?)$/\1/p' | $SED -rn 's:\W*(\S\S)\W*:0x\1\n:pg' | $SED -rn '/^0x/p' | $XXD -r -p | $SED 's/[^a-zA-Z0-9_-]//g' )"

case $1 in
	-eu) {
		trap '' 2
		echo -ne "Current train: $TRAINVERSION\n" | $TEE -i -a $LOG
		TARGETTRAIN=""
		if [[ "$TRAINVERSION" = *ER* || "$TRAINVERSION" = *EU* ]]; then
			echo -ne "Skipping. Your train is already EU!\n" | $TEE -i -a $LOG
			exit 0
		else
			echo -ne "Creating backup...\n" | $TEE -i -a $LOG
			. $thisdir/backup -a
			echo -ne "Searching the match for non EU train...\n" | $TEE -i -a $LOG
			case $TRAINVERSION in
				MHIG_*_VW*)
					TARGETTRAIN="MHIG_EU_VW_K1550" ;;
				MHIG_*_AU*)
					TARGETTRAIN="MHIG_EU_AU_K1555" ;;
				MHI2Q_??_AUG22*)
					TARGETTRAIN="MHI2Q_ER_AUG22_P5092" ;;
				MHI2_??_AUG22*)
					TARGETTRAIN="MHI2_ER_AUG22_K3346" ;;
				*AU276*)
					TARGETTRAIN="MHI2_ER_AU276_P5088" ;;
				*AU37x*)
					TARGETTRAIN="MHI2_ER_AU37x_P5089" ;;
				*AU43x*)
					TARGETTRAIN="MHI2_ER_AU43x_P5098" ;;
				*AUG11*|*AU57x*)
					TARGETTRAIN="MHI2_ER_AU57x_K3663" ;;
				*AU62x*)
					TARGETTRAIN="MHI2_ER_AU62x_P5099" ;;
				*POG11*)
					TARGETTRAIN="MHI2_ER_POG11_K5126" ;;
				*POG24*)
					TARGETTRAIN="MHI2_ER_POG24_K4224" ;;
				*SKG13*)
					TARGETTRAIN="MHI2_ER_SKG13_P4526" ;;
				*VWG11*|*JP_VW270_?1*|*CN_VW48x_?06*|*JP_VW48x_?06*)
					TARGETTRAIN="MHI2_ER_VWG11_K3342" ;;
				*VWG13*|*JP_VW48x_?07*|*CN_VW48x_?08*|*CN_VW48x_?09*|*CN_VW48x_?10*|*CN_VW48x_?11*|\
				*CN_VW48x_?12*|*JP_VW48x_?08*|*JP_VW48x_?09*|*JP_VW48x_?10*|*JP_VW48x_?11*|*JP_VW48x_?12*|\
				*TW_VW48x_?07*|*TW_VW48x_?09*|*TW_VW48x_?11*|*CN_VW511_?10*)
					TARGETTRAIN="MHI2_ER_VWG13_P4521" ;;
				*)
					echo -ne "\nAborted. Unknown train!\nTo add support, upload backup to https://mibsolution.one M.I.B_Backup\n" | $TEE -i -a $LOG
					exit 1 ;;
			esac
			echo -ne "Changing train to $TARGETTRAIN\n" | $TEE -i -a $LOG
			$E2P w 3a0 $(echo $TARGETTRAIN | awk '{print substr($1,1,16)}' 2>/dev/null | $XXD -p | $SED 's/../& /g' | $SED 's/0a //') >> $LOG
			if [[ "$TARGETTRAIN" = *MHI2Q* ]]; then
				$E2P w 3b0 $(echo $TARGETTRAIN | awk '{print substr($1,17,20)}' 2>/dev/null | $XXD -p | $SED 's/../& /g' | $SED 's/0a //') 20 >> $LOG
			else
				$E2P w 3b0 $(echo $TARGETTRAIN | awk '{print substr($1,17,19)}' 2>/dev/null | $XXD -p | $SED 's/../& /g' | $SED 's/0a //') 20 20 >> $LOG
			fi
		fi
		
if [[ "$TRAINVERSION" = *VW* ]] || [[ "$TRAINVERSION" = *SE* ]] || [[ "$TRAINVERSION" = *SK* ]]; then
	echo -ne "Setting byte17(skin)=1 and byte18(screen)=0 of 5F coding\n" | $TEE -a $LOG
	echo -ne "to avoid hang on the logo after update to EU fw\n" | $TEE -a $LOG
	$PERSW -f -t int 0 1343767807 1 >> $LOG #skin=0 (byte 17 of 5F coding)
else
	echo -ne "Zeroing bytes 17 and 18 of 5F coding to avoid hang on the logo in EU FW\n" | $TEE -a $LOG
	$PERSW -f -t int 0 1343767807 0 >> $LOG #skin=0 (byte 17 of 5F coding)
fi
$PERSW -f -t int 0 1343768831 0 >> $LOG #screen=0 (byte 18 of 5F coding)

echo -ne "\n$thisname done!\n" | $TEE -i -a $LOG

trap 2

[ -z "$noboot" ] && . $thisdir/reboot -t 10

[ -z "$noboot" ] && return 2> /dev/null

	};;

	# help or unknown parameter ------------------------------
	*) {
		echo ""
		echo $revision
		echo ""
		echo "Usage: "$thisname" [OPTION] [-noboot]"
		echo ""
		echo "Options:"
		echo "        -eu           set Train to EU"
		echo "        --help        show this help"
		echo ""
		echo "Note: Backup will only work in RCC bash!"
		echo ""
		echo "This program is free software; you can redistribute it and/or"
		echo "modify it under the terms of the GNU General Public License"
		echo "as published by the Free Software Foundation; either version 2"
		echo "of the License, or (at your option) any later version."
		echo ""
		echo "This program is distributed in the hope that it will be useful,"
		echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
		echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
		echo "See the GNU General Public License for more details."
		echo ""
		echo "You should have received a copy of the GNU General Public License"
		echo "along with this program; if not, write to the Free Software Foundation,"
		echo "Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA."
		echo ""
	};;
esac

exit 0
